#include "main.h"
#include "OLED.h"
#include "usart.h"
#include "GBK.h"
#include "stdio.h"
#include "string.h"
uint8_t Welcome_GBK[22] = {0xBB,0xB6,0xD3,0xAD,0xCA,0xB9,0xD3,0xC3,0xC3,0xFB,0xD1,0xD4,0xBE,0xAF,0xBE,0xE4,0xD4,0xC4,0xB6,0xC1,0xC6,0xF7};
uint8_t Welcome_buffer[32];
	
uint8_t Find_GKB(uint8_t buf1,uint8_t buf2,uint8_t *buffer)
{
	uint8_t res;
	res = f_open(&file,"0:/hzk16s",FA_OPEN_EXISTING|FA_READ);
	if(res != 0)
	{
		HAL_UART_Transmit(&huart1,(uint8_t*)"打不开",sizeof((uint8_t*)"打不开"),0xffff);
		return 1;
	}
	
	f_lseek(&file,32*((buf1 -0xa0-1)*94+(buf2-0xa0-1)));
	f_read(&file,buffer,32,&br);
	
	f_close(&file);
	return 0;
}

void Welcome_GUI()
{
	for(int i = 0;i < 22;i=i+2)
	{
		Find_GKB(Welcome_GBK[i],Welcome_GBK[i+1],Welcome_buffer);
		print_HZK16_string(Welcome_buffer,32);
	}
}

int change_arrangement_mode(uint8_t *pc_buf, uint8_t *oled_buf, int len)
{
    int i, j, k;

    if (len % 32)
    {
        return -1;
    }

    k = 0;
    do {
        for (i = 0; i < 8; i++)
        {
            for (j = 0; j < 8; j++)
            {
                oled_buf[i] |= ( ((pc_buf[j * 2] & (0x1 << 7 - (i % 8))) >> (7 - (i % 8)) ) << j );
                oled_buf[i + 8] |= ( ((pc_buf[j * 2 + 1] & (0x1 << 7 - (i % 8))) >> (7 - (i % 8)) ) << j );
                oled_buf[i + 16] |= ( ((pc_buf[j * 2 + 16] & (0x1 << 7 - (i % 8))) >> (7 - (i % 8)) ) << j );
                oled_buf[i + 24] |= ( ((pc_buf[j * 2 + 1 + 16] & (0x1 << 7 - (i % 8))) >> (7 - (i % 8)) ) << j 	);
                //printf("buf[%d] bit[%d] = byte[%d] & %0x << %d \n", i, j, j * 2, (0x1 << 7 - (i % 8)), j);
            }
         }
         k += 32;
         pc_buf += 32;
         oled_buf += 32;
     }while(k < len);

    pc_buf -= k;
    oled_buf -= k;

    return 0;
}
